<?php

namespace app\wechat\models;

use app\wechat\core\BaseModel;
use cm;
use Yii;
use yii\helpers\Url;

class ShiftModel extends BaseModel
{
	/*
	*查询班次
	*/
	public function actionSearch(){
		$newtime = time();
		$page = Cm::$req->post('page', ['null', 'empty', 'default' => 1]);
		$limit = Cm::$req->post('limit', ['null', 'empty', 'default' => 10]);
		$sid = (int)Cm::$req->post('sid', ['null', 'empty', 'default' => 0]);
		$eid = (int)Cm::$req->post('eid', ['null', 'empty', 'default' => 0]);
		$stype = Cm::$req->post('stype', ['null', 'empty', 'default' => '']);
		$etype = Cm::$req->post('etype', ['null', 'empty', 'default' => '']);
		$time = Cm::$req->post('time', ['null', 'empty', 'default' => date("Y-m-d",$newtime)]);
		$where = $this->getDataWhere("",$sid,$eid,$stype,$etype,$time,date("Y-m-d H:i:s",$newtime));
		$sql = "SELECT s.id,coa.name AS citysname,cob.name AS cityename,sa.name AS sitesname,sb.name AS siteename,s.s_ticket,s.departuretime,s.closingtime,s.presaletime,s.ticket_price,s.circuit_id FROM circuit AS a 
				LEFT JOIN open_city coa ON coa.code=a.city_scode 
				LEFT JOIN open_city cob ON cob.code=a.city_ecode
				LEFT JOIN site sa ON sa.id=a.site_sid 
				LEFT JOIN site sb ON sb.id=a.site_eid 
				LEFT JOIN shift s ON s.circuit_id=a.id
				WHERE a.status=1 AND s.status=1 AND coa.status=1 AND cob.status=1 AND sa.status=1 AND sb.status=1 
				{$where}
				ORDER BY s.departuretime ASC,a.is_hot DESC";
		$resdata = Cm::$db->paging($sql, $page, $limit);
		if(!empty($resdata['data'])){
			$config = Cm::$db->all("SELECT `key`,`value` FROM config_setting WHERE `group`='sys' AND `key` IN ('sys_service_charge','sys_premium') "); 
			$service_charge = 0;
			$premium = 0;
			if(!empty($config)){
				foreach ($config as $key => $value) {
					if($value['key'] == 'sys_service_charge'){
						$service_charge = floatval($value['value']);	
					}else if($value['key'] == 'sys_premium'){
						$premium = floatval($value['value']);	
					}
				}
			}
			foreach ($resdata['data'] as $key => &$value) {
				$departuretime = strtotime($value['departuretime']);
				$value['day'] = date("m月d日",$departuretime);
				$value['hour'] = date("H:i",$departuretime);
				$value['ticket_price'] += $premium+$service_charge;
				$value['s_ticket'] = $this->checkSticket($value['circuit_id'],$value['id'],$value['s_ticket']);
			}
		}	 		
		Cm::$res->susess($resdata);
	}

	/*
	*开单班次详情
	*/
	public function actionInfo(){
		$id = (int)Cm::$req->post('id', ['null', 'empty', 'default' => 0]);
		$type = (int)Cm::$req->post('type', ['null', 'empty', 'default' => 0]);
		$order_id = (int)Cm::$req->post('order_id', ['null', 'empty', 'default' => 0]);
		if(empty($id)){
			Cm::$res->error('请选择出发线路和班次');
		}
		$sql = "SELECT s.id,s.circuit_id,s.min,s.max,s.type,a.mileage,coa.name AS citysname,cob.name AS cityename,sa.name AS sitesname,sb.name AS siteename,s.s_ticket,s.departuretime,s.closingtime,s.presaletime,s.ticket_price,a.s_address,a.e_address 
				FROM shift AS s 
				INNER JOIN circuit a ON s.circuit_id=a.id
				INNER JOIN open_city coa ON coa.code=a.city_scode 
				INNER JOIN open_city cob ON cob.code=a.city_ecode
				INNER JOIN site sa ON sa.id=a.site_sid 
				INNER JOIN site sb ON sb.id=a.site_eid 
				WHERE s.id={$id} AND a.status=1 AND s.status=1 AND coa.status=1 AND cob.status=1 AND sa.status=1 AND sb.status=1";
		$info = Cm::$db->one($sql);
		if(empty($info)){
			Cm::$res->error('该班次未找打');	
		}
		$config = Cm::$db->all("SELECT `key`,`value` FROM config_setting WHERE `group`='sys' AND `key` IN ('sys_service_charge','sys_premium') "); 
		if($type == 1 && $order_id != 0){
			$passenger = Cm::$db->all("SELECT name,telephone,id_card AS idcard FROM order_passenger WHERE order_id={$order_id} AND status=1");
			/*foreach ($passenger as $key => &$value) {
				$idcardlen = strlen($value['idcard']);
				$value['idcard'] = $idcardlen==15?substr_replace($value['idcard'],"******",4,6):($idcardlen==18?substr_replace($value['idcard'],"*********",4,9):$value['idcard']);
			}*/
			$info['passenger'] = $passenger;
		}else{
			$info['passenger'] = [];
		}
		$info['service_charge'] = 0;
		$info['premium'] = 0;
		if(!empty($config)){
			foreach ($config as $key => $value) {
				if($value['key'] == 'sys_service_charge'){
					$info['service_charge'] = floatval($value['value']);	
				}else if($value['key'] == 'sys_premium'){
					$info['premium'] = floatval($value['value']);	
				}
			}
		}
		$info['ticket_price'] += $info['premium']+$info['service_charge'];
		$info['s_ticket'] = $this->checkSticket($info['circuit_id'],$info['id'],$info['s_ticket']);
		$closingtime = strtotime($info['closingtime']);
		$departuretime = strtotime($info['departuretime']);
		$time = time();
		if($closingtime < $time){
			Cm::$res->error('该班次已停售');		
		}
		if($departuretime < $time){
			Cm::$res->error('该班次已发车');		
		}
		$info['day'] = date("m月d日",$departuretime);
		$info['hour'] = date("H:i",$departuretime);
		$info['type'] = $info['type'] == 1 ? '固定班' : '流动班';
		$info['s_address'] = empty($info['s_address']) ? '' : $info['s_address'];
		$info['e_address'] = empty($info['e_address']) ? '' : $info['e_address']; 
		Cm::$res->susess($info);		
	}

	/*
	*构造查询条件
	*/
	private function getDataWhere($where = "",$sid,$eid,$stype,$etype,$time,$newtime){
		$where = $this->whereTypeId($where,$sid,$stype,'sa','coa');
		$where = $this->whereTypeId($where,$eid,$etype,'sb','cob');
		$stime = $time." 00:00:00";
		$etime = $time." 23:59:59";
		$where .= " AND s.departuretime>='{$stime}' AND s.departuretime>'{$newtime}' AND s.departuretime<='{$etime}' ";
		return $where;
	}

	/*
	*判断起始终点类型的ID查询条件
	*/
	private function whereTypeId($where,$id,$type,$sitefield,$cityfield){
		switch ($type) {
			case 'site':
				$where .= empty($id) ? "" : " AND {$sitefield}.id={$id} ";
				break;
			case 'city':
				$where .= empty($id) ? "" : " AND {$cityfield}.id={$id} ";
				break;	
			default:
				$where .= empty($id) ? "" : " AND ( {$cityfield}.id={$id} OR {$sitefield}.id={$id} ) ";
				break;
		}
		return $where;
	}
}